<?php
namespace app\admin\controller;

class Finance extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function dayIncome() {
        $role = $this->role;
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        $offset = ($page - 1) * $pageSize;
        
        $where = [];
        
        $startTime = isset($params['start']) ? trim($params['start']) : '';
        $endTime = isset($params['end']) ? trim($params['end']) : '';
        if($startTime === $endTime && $startTime !== '') { //查询当日
            $startTime = strtotime($startTime);
            $endTime = strtotime('+ 1 day', $startTime);
            $where[] = ['day', '>=', $startTime];
            $where[] = ['day', '<', $endTime];
        }else{
            if($startTime !== '') {
                $startTime = strtotime($startTime);
                $where[] = ['day', '>=', $startTime];
            }
            
            if($endTime !== '') {
                $endTime = strtotime($endTime);
                $where[] = ['day', '<', $endTime];
            }
            
        }
        
        switch ($role) {
            case 1 : $model = model('PlatformDayIncome'); break;
            case 2 : $model = model('AgentDayIncome'); $where[] = ['agent_id', '=', $this->userid]; break;
            case 2 : $model = model('AgentDayIncome'); $where[] = ['store_id', '=', $this->userid]; break;
        }
        
        $repData = [];
        
        $repData['list'] = $model->where($where)->order('id desc')->limit($offset, $pageSize)->select()->toArray();
        if(isset($params['init']) && $params['init'] == 1){
            $repData['total'] = $model->where($where)->count();
        }
        
        return successResult($repData);
    }
    
    public function incomeList() {
        $role = $this->role;
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        $offset = ($page - 1) * $pageSize;
        
        $where = [];
        
        $startTime = isset($params['start']) ? trim($params['start']) : '';
        $endTime = isset($params['end']) ? trim($params['end']) : '';
        if($startTime === $endTime && $startTime !== '') { //查询当日
            $startTime = strtotime($startTime);
            $endTime = strtotime('+ 1 day', $startTime);
            $where[] = ['day', '>=', $startTime];
            $where[] = ['day', '<', $endTime];
        }else{
            if($startTime !== '') {
                $startTime = strtotime($startTime);
                $where[] = ['day', '>=', $startTime];
            }
            
            if($endTime !== '') {
                $endTime = strtotime($endTime);
                $where[] = ['day', '<', $endTime];
            }
            
        }
        
        $repData = [];
        
        switch ($role) {
            case 1 : $model = model('PlatformFinanceLog'); break;
            case 2 : $model = model('AgentFinanceLog'); $where[] = ['agent_id', '=', $this->userid]; break;
            case 2 : $model = model('StoreFinanceLog'); $where[] = ['store_id', '=', $this->userid]; break;
        }
        
        
        $repData['list'] = $model->where($where)->order('id desc')->limit($offset, $pageSize)->select()->toArray();
        if(isset($params['init']) && $params['init'] == 1){
            $repData['total'] = $model->where($where)->count();
        }
        
        return successResult($repData);
    }
}






